package com.googlecode.maurersmarbles;

public class Move {

	/**
	 * The marble number.
	 */
	private int marbleNumber;
	/**
	 * The new board index.
	 */
	private int newBoardIndex;
	/**
	 * The player number.
	 */
	private int playerNumber;

	/**
	 * Creates a new Move.
	 * @param playerNumber the player number
	 * @param marbleNumber the marble number
	 * @param newBoardIndex the new board index
	 */
	public Move(int playerNumber, int marbleNumber, int newBoardIndex) {
		this.playerNumber = playerNumber;
		this.marbleNumber = marbleNumber;
		this.newBoardIndex = newBoardIndex;
	}

	/**
	 * Creates a new Move.
	 * @param player the player
	 * @param marble the marble
	 * @param newBoardIndex the new board index
	 */
	public Move(Player player, Marble marble, int newBoardIndex) {
		this.playerNumber = player.getPlayerNumber();
		this.marbleNumber = marble.getMarbleNumber();
		this.newBoardIndex = newBoardIndex;
	}

	/**
	 * Returns the marble number to move.
	 * @return the marble number to move
	 */
	public int getMarbleNumber() {
		return marbleNumber;
	}

	/**
	 * Returns the new board index.
	 * @return the new board index
	 */
	public int getNewBoardIndex() {
		return newBoardIndex;
	}

	/**
	 * Returns the player number.
	 * @return the player number
	 */
	public int getPlayerNumber() {
		return playerNumber;
	}

	/**
	 * Returns whether or not the two moves are for the same player and marble.
	 * @param move1 the first move
	 * @param move2 the second move
	 * @return whether or not the two moves are for the same player and marble
	 */
	public static boolean isSamePlayerAndMarble(Move move1, Move move2) {

		if (move1 == null || move2 == null) {
			return false;
		}

		return (move1.getPlayerNumber() == move2.getPlayerNumber())
				&& (move1.getMarbleNumber() == move2.getMarbleNumber());

	}

	/**
	 * Returns whether or not the given player number and marble number are the
	 * same as the player number and marble number for this move.
	 * @param playerNumber the player number
	 * @param marbleNumber the marble number
	 * @return whether or not the given player number and marble number are the
	 *         same as the player number and marble number for this move
	 */
	public boolean isSamePlayerAndMarble(int playerNumber, int marbleNumber) {
		return this.playerNumber == playerNumber
				&& this.marbleNumber == marbleNumber;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + marbleNumber;
		result = prime * result + newBoardIndex;
		result = prime * result + playerNumber;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Move other = (Move) obj;
		if (marbleNumber != other.marbleNumber) {
			return false;
		}
		if (newBoardIndex != other.newBoardIndex) {
			return false;
		}
		if (playerNumber != other.playerNumber) {
			return false;
		}
		return true;
	}

}
